package com.ruoyi.nucleicacid.service.impl;

import java.util.Date;
import java.util.List;

import com.ruoyi.basics.domain.Building;
import com.ruoyi.basics.domain.Community;
import com.ruoyi.basics.domain.Household;
import com.ruoyi.basics.mapper.BuildingMapper;
import com.ruoyi.basics.mapper.CommunityMapper;
import com.ruoyi.basics.mapper.HouseholdMapper;
import com.ruoyi.nucleicacid.domain.Householdertask;
import com.ruoyi.nucleicacid.mapper.HouseholdertaskMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.query.Param;
import org.springframework.security.core.parameters.P;
import org.springframework.stereotype.Service;
import com.ruoyi.nucleicacid.mapper.BuildingtaskMapper;
import com.ruoyi.nucleicacid.domain.Buildingtask;
import com.ruoyi.nucleicacid.service.IBuildingtaskService;

/**
 * 楼栋核酸任务Service业务层处理
 *Long buildingId = buildingtask.getBuildingId();
 *         Long communityI
 * @author kun
 * @date 2022-10-07
 */
@Service
public class BuildingtaskServiceImpl implements IBuildingtaskService
{
    @Autowired
    private BuildingtaskMapper buildingtaskMapper;

    @Autowired
    private HouseholdMapper householdMapper;

    @Autowired
    private CommunityMapper communityMapper;
    @Autowired
    private BuildingMapper buildingMapper;

    @Autowired
    private HouseholdertaskMapper householdertaskMapper;

    /**
     * 查询楼栋核酸任务
     *
     * @param id 楼栋核酸任务主键
     * @return 楼栋核酸任务
     */
    @Override
    public Buildingtask selectBuildingtaskById(Long id)
    {
        return buildingtaskMapper.selectBuildingtaskById(id);
    }

    /**
     * 查询楼栋核酸任务列表
     *
     * @param buildingtask 楼栋核酸任务
     * @return 楼栋核酸任务
     */
    @Override
    public List<Buildingtask> selectBuildingtaskList(Buildingtask buildingtask)
    {
        return buildingtaskMapper.selectBuildingtaskList(buildingtask);
    }

    /**
     * 新增楼栋核酸任务
     *
     * @param buildingtask 楼栋核酸任务
     * @return 结果
     */
    @Override
    public void insertBuildingtask(Buildingtask buildingtask){
        Long buildingId = buildingtask.getBuildingId();
        Long communityId = buildingtask.getCommunityId();
        Integer number = householdMapper.selectHouseholdBybuildingIdAndcommunityId(buildingId,communityId);
        buildingtask.setFinishnumber(0L);
        buildingtask.setTasknumber(number.longValue());
        if (new Date().getTime()>buildingtask.getStarttime().getTime() && new Date().getTime()< buildingtask.getEndtime().getTime()){
            buildingtask.setStatus(1L);
        }else if(new Date().getTime()<buildingtask.getStarttime().getTime()){
            buildingtask.setStatus(0L);
        }else {
            buildingtask.setStatus(2L);
        }

        Community community = communityMapper.selectCommunityById(communityId);
        Building building = buildingMapper.selectBuildingById(buildingId);

        List<Household> households = householdMapper.selectBybuildingIdAndcommunityId(buildingId,communityId);
        for (Household household : households){
            Householdertask householdertask = new Householdertask();
            householdertask.setTaskname(community.getName()+building.getBuildingname()+buildingtask.getTaskname());
            householdertask.setStatus(0L);
            householdertask.setName(household.getHouseholdname());
            householdertask.setStarttime(buildingtask.getStarttime());
            householdertask.setEndtime(buildingtask.getEndtime());
            householdertask.setDetectionposition(buildingtask.getDetectionposition());

            householdertaskMapper.insertHouseholdertask(householdertask);
        }

       buildingtaskMapper.insertBuildingtask(buildingtask);

    }

    /**
     * 修改楼栋核酸任务
     *
     * @param buildingtask 楼栋核酸任务
     * @return 结果
     */
    @Override
    public int updateBuildingtask(Buildingtask buildingtask)
    {
        return buildingtaskMapper.updateBuildingtask(buildingtask);
    }

    /**
     * 批量删除楼栋核酸任务
     *
     * @param ids 需要删除的楼栋核酸任务主键
     * @return 结果
     */
    @Override
    public int deleteBuildingtaskByIds(Long[] ids)
    {
        return buildingtaskMapper.deleteBuildingtaskByIds(ids);
    }

    /**
     * 删除楼栋核酸任务信息
     *
     * @param id 楼栋核酸任务主键
     * @return 结果
     */
    @Override
    public int deleteBuildingtaskById(Long id)
    {
        return buildingtaskMapper.deleteBuildingtaskById(id);
    }
}
